/**
 * 自动生成 WxEventCupid
 * 
 * Public/js/mp/wx_event_cupid.js
 * Zix @ 2015-08-12
 */

var WxEventCupid = {
  config: {
    editor : null ,
  },
  init: function (config) {
    var _this = this;
    $.extend(_this.config, config);
    Layout.set_menu_select(_this.config.menu_id);
    $('.dtp').datetimepicker(DTP_DATE_OPTION);
    
    loading();
    _this.init_btn();
    _this.init_grid();
    
    
    _this.config.editor = KindEditor.create('textarea[name="rule"]' , KE_Setting );
    
    sessionStorage.removeItem("scroll_top");
  },
  
  set_modal: function () {
    var _this = this;
    var scroll_top = sessionStorage.getItem("scroll_top");
    switch (_this.config.action) {
      case 'read' :
        $('#read_modal').show();
        $('#add_edit_modal').hide();

        if (isset(scroll_top)) {
          $(window).scrollTop(scroll_top);
        }
        break;
      case 'create':
      case 'update':
        sessionStorage.setItem('scroll_top', $(window).scrollTop());
        $('#read_modal').hide();
        $('#add_edit_modal').show();
        break;
    }
  },
  
  init_btn: function () {
    var _this = this;

    //封面
    $('#icon_upload_btn').Huploadify({
      uploader : _this.config.upload_icon_url , //上传文件
      itemTemplate : '',
      onUploadSuccess : function(file, responseText){
        var ret = $.parseJSON( responseText );
        tips( ret.info );
        if ( ret.status ) {
          _this.set_img_preview('icon' , ret.data.savePath );
        }
      }
    });

    //查询按钮
    $(document).on('click', '#search_btn', function (e) {
      e.preventDefault();
      var query_param = $('#data_table').TableGrid('getparam');
      query_param.page = 1;

      $('#data_table').TableGrid('param', query_param);
      $('#data_table').TableGrid('reload');
    });
    
    //关闭新增或编辑
    $(document).on('click', '#add_edit_close_btn', function (e) {
      e.preventDefault();
      _this.config.action = 'read';
      _this.set_modal();
    });
    
    //打开添加窗口
    $(document).on('click', '.load_add_modal_btn', function (e) {
      e.preventDefault();
      _this.config.action = 'create';
      $('#submit_form').attr('action', _this.config.create_url);
      $('#submit_form')[0].reset();
      _this.config.editoreditor.html('');
      _this.set_img_preview('icon' , '' );
      fillform( $('#submit_form') , _this.config.row );
      
      $('#action_label').html('新增');
      $('#add_edit_modal').removeClass('yellow').addClass('red');
      _this.set_modal();
    });

    //打开编辑窗口
    $(document).on('click', '.edit_btn', function (e) {
      e.preventDefault();
      
      var index = $('.edit_btn').index( $(this) );
      var data = $('#data_table').TableGrid('getrow' , index);

      fillform( $('#submit_form') , data );
      _this.config.editor.html(data.rule);
      _this.set_img_preview('icon' , data.icon );
      $('#submit_form').attr('action', _this.config.update_url + '/' + data['id'] );
      _this.config.action = 'update';
            
      $('#action_label').html('编辑');
      $('#add_edit_modal').removeClass('red').addClass('yellow');
      _this.set_modal();  
      
    });

    //提交
    $(document).on('click', '#submit_btn', function (e) {
      e.preventDefault();

      var _thisbtn = $(this);
      var url = $('#submit_form').attr('action');
      var data = $('#submit_form').serializeObject();

      if (validform($('#submit_form'))) {
        $.post(url, data, function (ret) {
          tips(ret.info) ;
          if (ret.status) {
            _this.config.action = 'read';
            _this.set_modal();
            var query_param = $('#data_table').TableGrid('getparam');
            query_param.page = 1 ;
            $('#data_table').TableGrid('param' , query_param);
            $('#data_table').TableGrid('reload');
          }
        });
      }
    });
    
    //批量删除按钮
    $(document).on('click', '.del-btn', function (e) {
      e.preventDefault();
      var select_id = new Array();
      $("input[name='tablegrid-select[]']").each(function (index) {
        if ($(this).prop('checked')) {
          //取表数据
          var row = $('#data_table').TableGrid('getrow', index);
          select_id.push(row.id);
        }
      });

      if (select_id.length == 0) {
        tips('请选择数据');
      } else {
        _this.config.del_id = select_id.join(',');
        _this.del_data();
      }
    });

    //单个删除按钮
    $(document).on('click', '.del_one_btn', function (e) {
      e.preventDefault();
      var index = $('.del_one_btn').index( $(this) );
      var data = $('#data_table').TableGrid('getrow' , index);
      _this.config.del_id = data['id'];
      _this.del_data();
    });
  },
  
  init_grid: function () {
    var _this = this;
    $('#data_table').TableGrid({
      url: _this.config.read_url,
      select_all: false,
      load_after_init: true,
      row_style: function (row, index) {
        if (row.status == 1 ) {
          return 'info';
        }
      }
    });
  },
  
  del_data: function (ids) {
    var _this = this;

    sure.init('确定删除吗？', function () {
      var url = _this.config.delete_url;
      var data = {ids: _this.config.del_id};

      $.post(url, data, function (ret) {
        tips(ret.info);
        if (ret.status) {
          $('#data_table').TableGrid('reload');
        }
      });
    });
  },
  
  set_img_preview : function(input_name , img_save_path , not_show_delete_btn ){
    if ( empty(img_save_path) ) {
      $('#'+ input_name +'_preview').html( '' );
      return ;
    }
    var img_ab_path = get_domain() + img_save_path ;
    $('input[name="'+ input_name +'"]').val( img_save_path );
    var img_html = '<a target="_blank" href="'+ img_ab_path +'" ><img src="'+ img_ab_path +'"></a>' ;
    var del_btn_html = '';
    if ( !not_show_delete_btn ) {
      var del_btn_html = '<a href="'+ img_save_path +'" class="del-icon-btn" data-input-name="'+ input_name +'"><i class="fa fa-times "></i></a>';
    }
    $('#'+ input_name +'_preview').html( img_html + del_btn_html );
  }
};

var opt_more = function( value, row, index ){
  var html = '<div class="dropdown">';
  html += '<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">';
  html += '更多操作 <span class="caret"></span></button>';
  html += '<ul class="dropdown-menu">';
  html += '<li><a href="'+ WxEventCupid.config.beauty_url + row.id +'" taget="_blank" title="帅哥美女">帅哥美女</a></li>';
  html += '<li><a href="'+ WxEventCupid.config.user_url   + row.id +'" taget="_blank" title="参与用户">参与用户</a></li>';
  html += '<li><a href="'+ WxEventCupid.config.award_url  + row.id +'" taget="_blank" title="活动奖品">活动奖品</a></li>';
  html += '<li></li>';
  html += '<li role="separator" class="divider"></li>';
  html += '<li><a href="'+ WxEventCupid.config.stat_url + row.id +'" taget="_blank" title="活动统计">活动统计</a></li>';
  html += '</ul></div>';
  return html ;
}

var opt_beauty = function(value, row, index) {
	return '<a class="btn btn-info user_btn" data-id="' + row.id
			+ '" href="'+ WxEventCupid.config.beauty_url + row.id +'" taget="_blank" title="帅哥美女"><i class="fa fa-user "></i> 帅哥美女</a>';
}

var opt_user = function(value, row, index) {
	return '<a class="btn btn-primary user_btn" data-id="' + row.id
			+ '" href="'+ WxEventCupid.config.user_url + row.id +'" taget="_blank" title="参与用户"><i class="fa fa-user "></i> 参与用户</a>';
}

var opt_award = function(value, row, index) {
	return '<a class="btn btn-success award_btn" data-id="' + row.id
			+ '" href="'+ WxEventCupid.config.award_url + row.id +'" taget="_blank" title="活动奖品"><i class="fa fa-gift "></i> 活动奖品</a>';
}




